/*
 * traininfo_dao.c
 *
 *  Created on: 2013-2-14
 *      Author: panqiuhui
 */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <mysql.h>

#include "../head/dao/traininfo_dao.h"

extern MYSQL_STMT *get_stationinfo_stmt;
extern MYSQL_STMT *get_traininfo_stmt;
extern MYSQL_STMT *get_train_through_stationsinfo_stmt;
extern MYSQL_STMT *get_start_end_stationsinfo_stmt;
extern MYSQL_STMT *get_stations_timeinfo_stmt;
extern MYSQL_STMT *get_throughstation_timeinfo_stmt;

enum {
	TABLE_STATIONS_SELECT_STATION_NAME = 0,
	TABLE_STATIONS_SELECT_CITY,
	TABLE_STATIONS_SELECT_STARTSALETIME,
	TABLE_STATIONS_SELECT_COLUMN_NUM
};

enum {
	TABLE_TRAINS_SELECT_TRAINID = 0,
	TABLE_TRAINS_SELECT_TYPE,
	TABLE_TRAINS_SELECT_DEPARTSTATION,
	TABLE_TRAINS_SELECT_ARRIVESTATION,
	TABLE_TRAINS_SELECT_DEPARTTIME,
	TABLE_TRAINS_SELECT_ARRIVETIME,
	TABLE_TRAINS_SELECT_CONSUMETIME,
	TABLE_TRAINS_SELECT_COLUMN_NUM
};

enum {
	TABLE_TRAINTHROUGHSTATIONS_SELECT_THROUGHSTATIONS = 0,
	TABLE_TRAINTHROUGHSTATIONS_SELECT_STATIONARRIVETIME,
	TABLE_TRAINTHROUGHSTATIONS_SELECT_STATIONDEPARTTIME,
	TABLE_TRAINTHROUGHSTATIONS_SELECT_CONSUMETIME,
	TABLE_TRAINTHROUGHSTATIONS_SELECT_PRICE,
	TABLE_TRAINTHROUGHSTATIONS_SELECT_COLUMN_NUM
};

enum {
	INPUT_TRAINTHROUGHSTATIONS_SELECT_TRAINID = 0,
	INPUT_TRAINTHROUGHSTATIONS_SELECT_DEPARTDATE,
	INPUT_TRAINTHROUGHSTATIONS_SELECT_DEPARTSTATION,
	INPUT_TRAINTHROUGHSTATIONS_SELECT_COLUMN_NUM
};

enum {
	INPUT_TRAINTHROUGHSTATIONS_SELECT_STARTSTATIONCITY = 0,
	INPUT_TRAINTHROUGHSTATIONS_SELECT_ENDSTATIONCITY,
	INPUT_TRAINTHROUGHSTATIONS_SELECT_DATE
};

enum {
	OUTPUT_RAINTHROUGHSTATIONS_SELECT_TRAINID = 0,
	OUTPUT_RAINTHROUGHSTATIONS_SELECT_DEPARTSTATION,
	OUTPUT_RAINTHROUGHSTATIONS_SELECT_ARRIVESTATION,
	OUTPUT_RAINTHROUGHSTATIONS_SELECT_PRICE,
	OUTPUT_RAINTHROUGHSTATIONS_SELECT_COLUMN_NUM
};

enum {
	INPUT_STATIONS_TIMEINFO_STARTSTATION_TRAINID = 0,
	INPUT_STATIONS_TIMEINFO_ENDSTATION_TRAINID,
	INPUT_STATIONS_TIMEINFO_STARTSTATION,
	INPUT_STATIONS_TIMEINFO_ENDSTATION,
	INPUT_STATIONS_TIMEINFO_COLUMN_NUM
};

enum {
	OUTPUT_STATIONS_TIMEINFO_STARTSTATION_DEPARTTINE = 0,
	OUTPUT_STATIONS_TIMEINFO_ENDSTATION_ARRIVETIME,
	OUTPUT_STATIONS_TIMEINFO_STARTSTATION_ARRIVETIME,
	OUTPUT_STATIONS_TIMEINFO_STARTSTATION_CONSUMETIME,
	OUTPUT_STATIONS_TIMEINFO_ENDSTATION_CONSUMETIME,
	OUTPUT_STATIONS_TIMEINFO_COLUMN_NUM
};

enum {
	INPUT_THROUGHSTATION_TIMEINFO_STATIONCITY = 0,
	INPUT_THROUGHSTATION_TIMEINFO_COLUMN_NUM
};

enum {
	OUTPUT_THROUGHSTATION_TIMEINFO_TRAINID = 0,
	OUTPUT_THROUGHSTATION_TIMEINFO_STATIONNAME,
	OUTPUT_THROUGHSTATION_TIMEINFO_STATIONARRIVETIME,
	OUTPUT_THROUGHSTATION_TIMEINFO_STATIONDEPARTTIME,
	OUTPUT_THROUGHSTATION_TIMEINFO_COLUMN_NUM
};

//输出车站信息
void output_stationinfo(StationItem item) {
	printf("-----------------------------------------------\n");
	printf("stationName: %s\n", item.stationName);
	printf("stationArriveTime :%s\n", item.stationArriveTime);
	printf("stationDepartTime :%s\n", item.stationDepartTime);
	printf("stationTimelast: %s\n", item.consumeTime);
	printf("stationPrice: %s\n", item.price);
	printf("-----------------------------------------------\n");
}

//查询某个车次的基本信息，成功返回true并将结果添加到traininfo中，失败返回false
bool get_traininfo(const char *trainID, struct traininfo *train) {
	bool result = true;
	//设置输入的参数信息
	MYSQL_BIND input_bind[1];
	char input_trainID[TRAINS_TRAINID_LEN];
	unsigned long input_trainID_len;

	memset(input_bind, 0, sizeof(input_bind));
	input_bind[TABLE_TRAINS_SELECT_TRAINID].buffer_type = MYSQL_TYPE_STRING;
	input_bind[TABLE_TRAINS_SELECT_TRAINID].buffer = input_trainID;
	input_bind[TABLE_TRAINS_SELECT_TRAINID].buffer_length = TRAINS_TRAINID_LEN;
	input_bind[TABLE_TRAINS_SELECT_TRAINID].is_null = 0;
	input_bind[TABLE_TRAINS_SELECT_TRAINID].length = &input_trainID_len;

	memset(input_trainID, 0, sizeof(input_trainID));
	strncpy(input_trainID, trainID, sizeof(input_trainID));
	input_trainID_len = strlen(input_trainID);

	//设置输出的参数信息
	MYSQL_BIND output_bind[TABLE_TRAINS_SELECT_COLUMN_NUM];
	char output_trainID[TRAINS_TRAINID_LEN];
	char output_type[TRAINS_TYPE_LEN];
	char output_departStation[TRAINS_DEPARTSTATIONS_LEN];
	char output_arriveStation[TRAINS_ARRIVESTATIONS_LEN];
	MYSQL_TIME output_departTime;
	MYSQL_TIME output_arriveTime;
	MYSQL_TIME output_consumeTime;
	my_bool output_is_null[TABLE_TRAINS_SELECT_COLUMN_NUM];
	my_bool output_error[TABLE_TRAINS_SELECT_COLUMN_NUM];
	unsigned long output_length[TABLE_TRAINS_SELECT_COLUMN_NUM];

	memset(output_bind, 0, sizeof(output_bind));
	output_bind[TABLE_TRAINS_SELECT_TRAINID].buffer_type = MYSQL_TYPE_STRING;
	output_bind[TABLE_TRAINS_SELECT_TRAINID].buffer = output_trainID;
	output_bind[TABLE_TRAINS_SELECT_TRAINID].buffer_length = TRAINS_TRAINID_LEN;

	output_bind[TABLE_TRAINS_SELECT_TYPE].buffer_type = MYSQL_TYPE_STRING;
	output_bind[TABLE_TRAINS_SELECT_TYPE].buffer = output_type;
	output_bind[TABLE_TRAINS_SELECT_TYPE].buffer_length = TRAINS_TYPE_LEN;

	output_bind[TABLE_TRAINS_SELECT_DEPARTSTATION].buffer_type =
			MYSQL_TYPE_STRING;
	output_bind[TABLE_TRAINS_SELECT_DEPARTSTATION].buffer =
			output_departStation;
	output_bind[TABLE_TRAINS_SELECT_DEPARTSTATION].buffer_length =
			TRAINS_DEPARTSTATIONS_LEN;

	output_bind[TABLE_TRAINS_SELECT_ARRIVESTATION].buffer_type =
			MYSQL_TYPE_STRING;
	output_bind[TABLE_TRAINS_SELECT_ARRIVESTATION].buffer =
			output_arriveStation;
	output_bind[TABLE_TRAINS_SELECT_ARRIVESTATION].buffer_length =
			TRAINS_ARRIVESTATIONS_LEN;

	output_bind[TABLE_TRAINS_SELECT_DEPARTTIME].buffer_type = MYSQL_TYPE_TIME;
	output_bind[TABLE_TRAINS_SELECT_DEPARTTIME].buffer = &output_departTime;

	output_bind[TABLE_TRAINS_SELECT_ARRIVETIME].buffer_type = MYSQL_TYPE_TIME;
	output_bind[TABLE_TRAINS_SELECT_ARRIVETIME].buffer = &output_arriveTime;

	output_bind[TABLE_TRAINS_SELECT_CONSUMETIME].buffer_type = MYSQL_TYPE_TIME;
	output_bind[TABLE_TRAINS_SELECT_CONSUMETIME].buffer = &output_consumeTime;

	int i = 0;
	for (i = 0; i < TABLE_TRAINS_SELECT_COLUMN_NUM; i++) {
		output_bind[i].is_null = &output_is_null[i];
		output_bind[i].length = &output_length[i];
		output_bind[i].error = &output_error[i];
	}

	if (mysql_stmt_bind_param(get_traininfo_stmt, input_bind)) {
		fprintf(stderr, "get_traininfo_stmt mysql_stmt_bind_param() failed\n");
		fprintf(stderr, "%s\n", mysql_stmt_error(get_traininfo_stmt));
		exit(EXIT_FAILURE);
	}

	if (mysql_stmt_execute(get_traininfo_stmt)) {
		fprintf(stderr, "get_traininfo_stmt mysql_stmt_execute(), failed\n");
		fprintf(stderr, "%s\n", mysql_stmt_error(get_traininfo_stmt));
		exit(EXIT_FAILURE);
	}

	if (mysql_stmt_bind_result(get_traininfo_stmt, output_bind)) {
		fprintf(stderr, "get_traininfo_stmt mysql_stmt_bind_result() failed\n");
		fprintf(stderr, "%s\n", mysql_stmt_error(get_traininfo_stmt));
		exit(EXIT_FAILURE);
	}

	if (mysql_stmt_store_result(get_traininfo_stmt)) {
		fprintf(stderr,
				"get_traininfo_stmt mysql_stmt_store_result() failed\n");
		fprintf(stderr, "%s\n", mysql_stmt_error(get_traininfo_stmt));
		exit(EXIT_FAILURE);
	}

	int rownum = mysql_stmt_num_rows(get_traininfo_stmt);
	//printf("rownums: %d\n", rownum);

	if (rownum == 1) {
		while (!mysql_stmt_fetch(get_traininfo_stmt)) {
			if (!output_is_null[TABLE_TRAINS_SELECT_TRAINID]) {
				sprintf(train->trainID, "%s", output_trainID);
			}
			if (!output_is_null[TABLE_TRAINS_SELECT_TYPE]) {
				sprintf(train->type, "%s", output_type);
			}
			if (!output_is_null[TABLE_TRAINS_SELECT_DEPARTSTATION]) {
				sprintf(train->departStation, "%s", output_departStation);
			}
			if (!output_is_null[TABLE_TRAINS_SELECT_ARRIVESTATION]) {
				sprintf(train->arriveStation, "%s", output_arriveStation);
			}
			if (!output_is_null[TABLE_TRAINS_SELECT_DEPARTTIME]) {
				sprintf(train->departTime, "%02d:%02d", output_departTime.hour,
						output_departTime.minute);
			}
			if (!output_is_null[TABLE_TRAINS_SELECT_ARRIVETIME]) {
				sprintf(train->arriveTime, "%02d:%02d", output_arriveTime.hour,
						output_arriveTime.minute);
			}
			if (!output_is_null[TABLE_TRAINS_SELECT_CONSUMETIME]) {
				sprintf(train->consumeTime, "%02d:%02d",
						output_consumeTime.hour, output_consumeTime.minute);
			}
			break;
		}
	} else {
		result = false;
	}
	return result;
}

//查询某个车次经过的车站信息，成功返回true并将结果添加到stationlist中，失败返回false
bool search_train_number(const struct train_number_request *request,
		StationList *stationlist) {
	bool result = true;

	struct traininfo train;
	memset(&train, 0, sizeof(train));
	result = get_traininfo(request->trainID, &train); //先检测trainID对应的车次是否存在
	if (!result) {
		return result;
	}

	//设置输入的参数信息
	MYSQL_BIND input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_COLUMN_NUM];
	char input_trainID[TRAINS_TRAINID_LEN];
	unsigned long input_trainID_len = strlen(request->trainID);
	MYSQL_TIME input_departDate;
	char input_departStation[TRAINS_DEPARTSTATIONS_LEN];
	unsigned long input_departStation_len = strlen(train.departStation);

	memset(input_trainID, 0, sizeof(input_trainID));
	strncpy(input_trainID, request->trainID, strlen(request->trainID));

	int year;
	int month;
	int day;
	sscanf(request->date, "%04d-%02d-%02d", &year, &month, &day);
	input_departDate.year = year;
	input_departDate.month = month;
	input_departDate.day = day;

	memset(input_departStation, 0, sizeof(input_departStation));
	strncpy(input_departStation, train.departStation,
			strlen(train.departStation));

	memset(input_bind, 0, sizeof(input_bind));
	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_TRAINID].buffer_type =
			MYSQL_TYPE_STRING;
	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_TRAINID].buffer =
			input_trainID;
	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_TRAINID].buffer_length =
			TRAINS_TRAINID_LEN;
	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_TRAINID].is_null = 0;
	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_TRAINID].length =
			&input_trainID_len;

	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_DEPARTDATE].buffer_type =
			MYSQL_TYPE_DATE;
	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_DEPARTDATE].buffer =
			&input_departDate;
	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_DEPARTDATE].is_null = 0;

	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_DEPARTSTATION].buffer_type =
			MYSQL_TYPE_STRING;
	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_DEPARTSTATION].buffer =
			input_departStation;
	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_DEPARTSTATION].buffer_length =
			TRAINS_DEPARTSTATIONS_LEN;
	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_DEPARTSTATION].is_null = 0;
	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_DEPARTSTATION].length =
			&input_departStation_len;

	//设置输出的参数信息
	MYSQL_BIND output_bind[TABLE_TRAINTHROUGHSTATIONS_SELECT_COLUMN_NUM];
	char output_throughStation[TRAINS_DEPARTSTATIONS_LEN];
	MYSQL_TIME output_stationArriveTime;
	MYSQL_TIME output_stationDepartTime;
	MYSQL_TIME output_consumeTime;
	float output_price;
	unsigned long output_length[TABLE_TRAINTHROUGHSTATIONS_SELECT_COLUMN_NUM];
	my_bool output_is_null[TABLE_TRAINTHROUGHSTATIONS_SELECT_COLUMN_NUM];
	my_bool output_error[TABLE_TRAINTHROUGHSTATIONS_SELECT_COLUMN_NUM];

	memset(output_bind, 0, sizeof(output_bind));
	output_bind[TABLE_TRAINTHROUGHSTATIONS_SELECT_THROUGHSTATIONS].buffer_type =
			MYSQL_TYPE_STRING;
	output_bind[TABLE_TRAINTHROUGHSTATIONS_SELECT_THROUGHSTATIONS].buffer =
			output_throughStation;
	output_bind[TABLE_TRAINTHROUGHSTATIONS_SELECT_THROUGHSTATIONS].buffer_length =
			TRAINS_DEPARTSTATIONS_LEN;

	output_bind[TABLE_TRAINTHROUGHSTATIONS_SELECT_STATIONARRIVETIME].buffer_type =
			MYSQL_TYPE_TIME;
	output_bind[TABLE_TRAINTHROUGHSTATIONS_SELECT_STATIONARRIVETIME].buffer =
			&output_stationArriveTime;

	output_bind[TABLE_TRAINTHROUGHSTATIONS_SELECT_STATIONDEPARTTIME].buffer_type =
			MYSQL_TYPE_TIME;
	output_bind[TABLE_TRAINTHROUGHSTATIONS_SELECT_STATIONDEPARTTIME].buffer =
			&output_stationDepartTime;

	output_bind[TABLE_TRAINTHROUGHSTATIONS_SELECT_CONSUMETIME].buffer_type =
			MYSQL_TYPE_TIME;
	output_bind[TABLE_TRAINTHROUGHSTATIONS_SELECT_CONSUMETIME].buffer =
			&output_consumeTime;

	output_bind[TABLE_TRAINTHROUGHSTATIONS_SELECT_PRICE].buffer_type =
			MYSQL_TYPE_FLOAT;
	output_bind[TABLE_TRAINTHROUGHSTATIONS_SELECT_PRICE].buffer = &output_price;

	int i = 0;
	for (i = 0; i < TABLE_TRAINTHROUGHSTATIONS_SELECT_COLUMN_NUM; i++) {
		output_bind[i].is_null = &output_is_null[i];
		output_bind[i].length = &output_length[i];
		output_bind[i].error = &output_error[i];
	}

	if (mysql_stmt_bind_param(get_train_through_stationsinfo_stmt,
			input_bind)) {
		fprintf(stderr,
				"get_train_through_stationsinfo_stmt mysql_stmt_bind_param() failed\n");
		fprintf(stderr, "%s\n",
				mysql_stmt_error(get_train_through_stationsinfo_stmt));
		exit(EXIT_FAILURE);
	}

	if (mysql_stmt_execute(get_train_through_stationsinfo_stmt)) {
		fprintf(stderr,
				"get_train_through_stationsinfo_stmt mysql_stmt_execute(), failed\n");
		fprintf(stderr, "%s\n",
				mysql_stmt_error(get_train_through_stationsinfo_stmt));
		exit(EXIT_FAILURE);
	}

	if (mysql_stmt_bind_result(get_train_through_stationsinfo_stmt,
			output_bind)) {
		fprintf(stderr,
				"get_train_through_stationsinfo_stmt mysql_stmt_bind_result() failed\n");
		fprintf(stderr, "%s\n",
				mysql_stmt_error(get_train_through_stationsinfo_stmt));
		exit(EXIT_FAILURE);
	}

	if (mysql_stmt_store_result(get_train_through_stationsinfo_stmt)) {
		fprintf(stderr,
				"get_train_through_stationsinfo_stmt mysql_stmt_store_result() failed\n");
		fprintf(stderr, "%s\n",
				mysql_stmt_error(get_train_through_stationsinfo_stmt));
		exit(EXIT_FAILURE);
	}

	int rownum = mysql_stmt_num_rows(get_train_through_stationsinfo_stmt);
	//printf("rownums: %d\n", rownum);

	if (rownum > 0) {
		while (!mysql_stmt_fetch(get_train_through_stationsinfo_stmt)) {
			StationItem item;
			memset(&item, 0, sizeof(item));
			if (!output_is_null[TABLE_TRAINTHROUGHSTATIONS_SELECT_THROUGHSTATIONS]) {
				sprintf(item.stationName, "%s", output_throughStation);
			}

			if (!output_is_null[TABLE_TRAINTHROUGHSTATIONS_SELECT_STATIONARRIVETIME]) {
				sprintf(item.stationArriveTime, "%02d:%02d",
						output_stationArriveTime.hour,
						output_stationArriveTime.minute);
			} else {
				sprintf(item.stationArriveTime, "%s", "----");
			}

			if (!output_is_null[TABLE_TRAINTHROUGHSTATIONS_SELECT_STATIONDEPARTTIME]) {
				sprintf(item.stationDepartTime, "%02d:%02d",
						output_stationDepartTime.hour,
						output_stationDepartTime.minute);
			} else {
				sprintf(item.stationDepartTime, "%s", "----");
			}

			if (!output_is_null[TABLE_TRAINTHROUGHSTATIONS_SELECT_CONSUMETIME]) {
				sprintf(item.consumeTime, "%02d:%02d", output_consumeTime.hour,
						output_consumeTime.minute);
			} else {
				sprintf(item.consumeTime, "%s", "----");
			}

			if (!output_is_null[TABLE_TRAINTHROUGHSTATIONS_SELECT_PRICE]) {
				sprintf(item.price, "%-10.2f", output_price);
			} else {
				sprintf(item.price, "%s", "----");
			}

			AddItemStation(item, stationlist);
		}
	} else {
		result = false;
	}

	return result;
}

//查询发到站信息，成功返回true并将结果添加到start_end_stationlist中，失败返回false
bool search_start_end_station(
		const struct train_start_end_station_request *request,
		StartEndStationList *start_end_stationlist) {
	bool result = true;
	struct stationinfo start_station;
	struct stationinfo end_station;
	memset(&start_station, 0, sizeof(start_station));
	memset(&end_station, 0, sizeof(end_station));
	//查询发站和到站所在城市
	result = get_stationinfo(request->startStation, &start_station);
	if (!result) {
		return false;
	}
	result = get_stationinfo(request->endStation, &end_station);
	if (!result) {
		return false;
	}

	//从ticketsinfo表中查找符合条件车次，暂不考虑始发终到以及列车的类型
	//设置输入的参数信息
	MYSQL_BIND input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_COLUMN_NUM];
	char input_start_station_city[STATIONS_CITY_LEN];
	unsigned long input_start_station_city_len = strlen(start_station.city);
	char input_end_station_city[STATIONS_CITY_LEN];
	unsigned long input_end_station_city_len = strlen(end_station.city);
	MYSQL_TIME input_departDate;

	memset(input_start_station_city, 0, sizeof(input_start_station_city));
	strncpy(input_start_station_city, start_station.city,
			input_start_station_city_len);

	memset(input_end_station_city, 0, sizeof(input_end_station_city));
	strncpy(input_end_station_city, end_station.city,
			input_end_station_city_len);

	int year;
	int month;
	int day;
	sscanf(request->date, "%04d-%02d-%02d", &year, &month, &day);
	input_departDate.year = year;
	input_departDate.month = month;
	input_departDate.day = day;

	memset(input_bind, 0, sizeof(input_bind));
	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_STARTSTATIONCITY].buffer_type =
			MYSQL_TYPE_STRING;
	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_STARTSTATIONCITY].buffer =
			input_start_station_city;
	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_STARTSTATIONCITY].buffer_length =
			STATIONS_CITY_LEN;
	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_STARTSTATIONCITY].is_null = 0;
	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_STARTSTATIONCITY].length =
			&input_start_station_city_len;

	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_ENDSTATIONCITY].buffer_type =
			MYSQL_TYPE_STRING;
	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_ENDSTATIONCITY].buffer =
			input_end_station_city;
	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_ENDSTATIONCITY].buffer_length =
			STATIONS_CITY_LEN;
	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_ENDSTATIONCITY].is_null = 0;
	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_ENDSTATIONCITY].length =
			&input_end_station_city_len;

	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_DATE].buffer_type =
			MYSQL_TYPE_DATE;
	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_DATE].buffer =
			&input_departDate;
	input_bind[INPUT_TRAINTHROUGHSTATIONS_SELECT_DATE].is_null = 0;

	//设置输出的参数信息
	MYSQL_BIND output_bind[OUTPUT_RAINTHROUGHSTATIONS_SELECT_COLUMN_NUM];
	char output_trainID[TRAINS_TRAINID_LEN];
	char output_departStation[TRAINS_DEPARTSTATIONS_LEN];
	char output_arriveStation[TRAINS_ARRIVESTATIONS_LEN];
	float output_price;
	unsigned long output_length[OUTPUT_RAINTHROUGHSTATIONS_SELECT_COLUMN_NUM];
	my_bool output_is_null[OUTPUT_RAINTHROUGHSTATIONS_SELECT_COLUMN_NUM];
	my_bool output_error[OUTPUT_RAINTHROUGHSTATIONS_SELECT_COLUMN_NUM];

	memset(output_bind, 0, sizeof(output_bind));
	output_bind[OUTPUT_RAINTHROUGHSTATIONS_SELECT_TRAINID].buffer_type =
			MYSQL_TYPE_STRING;
	output_bind[OUTPUT_RAINTHROUGHSTATIONS_SELECT_TRAINID].buffer =
			output_trainID;
	output_bind[OUTPUT_RAINTHROUGHSTATIONS_SELECT_TRAINID].buffer_length =
			TRAINS_TRAINID_LEN;

	output_bind[OUTPUT_RAINTHROUGHSTATIONS_SELECT_DEPARTSTATION].buffer_type =
			MYSQL_TYPE_STRING;
	output_bind[OUTPUT_RAINTHROUGHSTATIONS_SELECT_DEPARTSTATION].buffer =
			output_departStation;
	output_bind[OUTPUT_RAINTHROUGHSTATIONS_SELECT_DEPARTSTATION].buffer_length =
			TRAINS_DEPARTSTATIONS_LEN;

	output_bind[OUTPUT_RAINTHROUGHSTATIONS_SELECT_ARRIVESTATION].buffer_type =
			MYSQL_TYPE_STRING;
	output_bind[OUTPUT_RAINTHROUGHSTATIONS_SELECT_ARRIVESTATION].buffer =
			output_arriveStation;
	output_bind[OUTPUT_RAINTHROUGHSTATIONS_SELECT_ARRIVESTATION].buffer_length =
			TRAINS_ARRIVESTATIONS_LEN;

	output_bind[OUTPUT_RAINTHROUGHSTATIONS_SELECT_PRICE].buffer_type =
			MYSQL_TYPE_FLOAT;
	output_bind[OUTPUT_RAINTHROUGHSTATIONS_SELECT_PRICE].buffer = &output_price;

	int i = 0;
	for (i = 0; i < OUTPUT_RAINTHROUGHSTATIONS_SELECT_COLUMN_NUM; i++) {
		output_bind[i].is_null = &output_is_null[i];
		output_bind[i].length = &output_length[i];
		output_bind[i].error = &output_error[i];
	}

	if (mysql_stmt_bind_param(get_start_end_stationsinfo_stmt, input_bind)) {
		fprintf(stderr,
				"get_start_end_stationsinfo_stmt mysql_stmt_bind_param() failed\n");
		fprintf(stderr, "%s\n",
				mysql_stmt_error(get_start_end_stationsinfo_stmt));
		exit(EXIT_FAILURE);
	}

	if (mysql_stmt_execute(get_start_end_stationsinfo_stmt)) {
		fprintf(stderr,
				"get_start_end_stationsinfo_stmt mysql_stmt_execute(), failed\n");
		fprintf(stderr, "%s\n",
				mysql_stmt_error(get_start_end_stationsinfo_stmt));
		exit(EXIT_FAILURE);
	}

	if (mysql_stmt_bind_result(get_start_end_stationsinfo_stmt, output_bind)) {
		fprintf(stderr,
				"get_start_end_stationsinfo_stmt mysql_stmt_bind_result() failed\n");
		fprintf(stderr, "%s\n",
				mysql_stmt_error(get_start_end_stationsinfo_stmt));
		exit(EXIT_FAILURE);
	}

	if (mysql_stmt_store_result(get_start_end_stationsinfo_stmt)) {
		fprintf(stderr,
				"get_start_end_stationsinfo_stmt mysql_stmt_store_result() failed\n");
		fprintf(stderr, "%s\n",
				mysql_stmt_error(get_start_end_stationsinfo_stmt));
		exit(EXIT_FAILURE);
	}

	int rownum = mysql_stmt_num_rows(get_start_end_stationsinfo_stmt);
	//printf("rownums: %d\n", rownum);

	if (rownum > 0) {
		while (!mysql_stmt_fetch(get_start_end_stationsinfo_stmt)) {
			StartEndStationItem item;
			memset(&item, 0, sizeof(item));
			if (!output_is_null[OUTPUT_RAINTHROUGHSTATIONS_SELECT_TRAINID]) {
				sprintf(item.trainID, "%s", output_trainID);
			}
			if (!output_is_null[OUTPUT_RAINTHROUGHSTATIONS_SELECT_DEPARTSTATION]) {
				sprintf(item.startStation, "%s", output_departStation);
			}
			if (!output_is_null[OUTPUT_RAINTHROUGHSTATIONS_SELECT_ARRIVESTATION]) {
				sprintf(item.endStation, "%s", output_arriveStation);
			}
			if (!output_is_null[OUTPUT_RAINTHROUGHSTATIONS_SELECT_PRICE]) {
				sprintf(item.price, "%-10.2f", output_price);
			}

			struct traininfo train;
			memset(&train, 0, sizeof(train));
			bool ishave = true;
			ishave = get_traininfo(item.trainID, &train); //得到一个车次的详细信息
			if (!ishave) { //如果没找到该车次信息，继续下一循环，现实中这种情况不应该存在
				continue;
			}

			bool is_ok = true;
			is_ok = check_train_type(request->trainType, train.trainID); //检查车次是否符合类型条件
			if (!is_ok) {
				continue;
			}
			is_ok = check_depart_arrive_station(request->passStationType,
					&train, input_start_station_city, input_end_station_city); //检查车次是否符合始发终到条件
			if (!is_ok) {
				continue;
			}

			struct stations_timeinfo timeinfo;
			memset(&timeinfo, 0, sizeof(timeinfo));
			is_ok = get_start_end_consume_timeinfo(train.trainID,
					output_departStation, output_arriveStation, &timeinfo); //得到车站的发时、到时以及历时信息
			if (!is_ok) {
				continue;
			}

			sprintf(item.startTime, "%s", timeinfo.startTime);
			sprintf(item.endTime, "%s", timeinfo.endTime);
			sprintf(item.consumeTime, "%s", timeinfo.consumeTime);
			sprintf(item.departStation, "%s", train.departStation);
			sprintf(item.arriveStation, "%s", train.arriveStation);

			AddItemStartEndStation(item, start_end_stationlist);
		}
		if (ListItemCountStartEndStation(start_end_stationlist) == 0) {
			result = false;
		}
	} else {
		result = false;
	}
	return result;
}

//查询车站经过车次信息，成功返回true并将结果添加到station_pass_trainlist中，失败返回false
bool search_station_pass_train(const struct station_pass_train_request *request,
		StationPassTrainList *station_pass_trainlist) {
	bool result = true;
	struct stationinfo station;
	memset(&station, 0, sizeof(station));
	//查询车站所在城市
	result = get_stationinfo(request->stationName, &station);
	if (!result) {
		return false;
	}

	//设置输入的参数信息
	MYSQL_BIND input_bind[INPUT_THROUGHSTATION_TIMEINFO_COLUMN_NUM];
	char input_city[STATIONS_CITY_LEN];
	unsigned long input_city_len;

	memset(input_city, 0, sizeof(input_city));
	strncpy(input_city, station.city, strlen(station.city));
	input_city_len = strlen(input_city);

	memset(input_bind, 0, sizeof(input_bind));
	input_bind[INPUT_THROUGHSTATION_TIMEINFO_STATIONCITY].buffer_type =
			MYSQL_TYPE_STRING;
	input_bind[INPUT_THROUGHSTATION_TIMEINFO_STATIONCITY].buffer = input_city;
	input_bind[INPUT_THROUGHSTATION_TIMEINFO_STATIONCITY].buffer_length =
			STATIONS_CITY_LEN;
	input_bind[INPUT_THROUGHSTATION_TIMEINFO_STATIONCITY].is_null = 0;
	input_bind[INPUT_THROUGHSTATION_TIMEINFO_STATIONCITY].length =
			&input_city_len;

	//设置输出的参数信息
	MYSQL_BIND output_bind[OUTPUT_THROUGHSTATION_TIMEINFO_COLUMN_NUM];
	char output_trainID[TRAINS_TRAINID_LEN];
	char output_stationName[STATIONS_STATION_NAME_LEN];
	MYSQL_TIME output_arriveTime;
	MYSQL_TIME output_departTime;
	unsigned long output_length[OUTPUT_THROUGHSTATION_TIMEINFO_COLUMN_NUM];
	my_bool output_is_null[OUTPUT_THROUGHSTATION_TIMEINFO_COLUMN_NUM];
	my_bool output_error[OUTPUT_THROUGHSTATION_TIMEINFO_COLUMN_NUM];

	memset(output_bind, 0, sizeof(output_bind));
	output_bind[OUTPUT_THROUGHSTATION_TIMEINFO_TRAINID].buffer_type =
			MYSQL_TYPE_STRING;
	output_bind[OUTPUT_THROUGHSTATION_TIMEINFO_TRAINID].buffer = output_trainID;
	output_bind[OUTPUT_THROUGHSTATION_TIMEINFO_TRAINID].buffer_length =
			TRAINS_TRAINID_LEN;

	output_bind[OUTPUT_THROUGHSTATION_TIMEINFO_STATIONNAME].buffer_type =
			MYSQL_TYPE_STRING;
	output_bind[OUTPUT_THROUGHSTATION_TIMEINFO_STATIONNAME].buffer =
			output_stationName;
	output_bind[OUTPUT_THROUGHSTATION_TIMEINFO_STATIONNAME].buffer_length =
			STATIONS_STATION_NAME_LEN;

	output_bind[OUTPUT_THROUGHSTATION_TIMEINFO_STATIONARRIVETIME].buffer_type =
			MYSQL_TYPE_TIME;
	output_bind[OUTPUT_THROUGHSTATION_TIMEINFO_STATIONARRIVETIME].buffer =
			&output_arriveTime;

	output_bind[OUTPUT_THROUGHSTATION_TIMEINFO_STATIONDEPARTTIME].buffer_type =
			MYSQL_TYPE_TIME;
	output_bind[OUTPUT_THROUGHSTATION_TIMEINFO_STATIONDEPARTTIME].buffer =
			&output_departTime;

	int i = 0;
	for (i = 0; i < OUTPUT_THROUGHSTATION_TIMEINFO_COLUMN_NUM; i++) {
		output_bind[i].is_null = &output_is_null[i];
		output_bind[i].length = &output_length[i];
		output_bind[i].error = &output_error[i];
	}

	if (mysql_stmt_bind_param(get_throughstation_timeinfo_stmt, input_bind)) {
		fprintf(stderr,
				"get_throughstation_timeinfo_stmt mysql_stmt_bind_param() failed\n");
		fprintf(stderr, "%s\n",
				mysql_stmt_error(get_throughstation_timeinfo_stmt));
		exit(EXIT_FAILURE);
	}

	if (mysql_stmt_execute(get_throughstation_timeinfo_stmt)) {
		fprintf(stderr,
				"get_throughstation_timeinfo_stmt mysql_stmt_execute(), failed\n");
		fprintf(stderr, "%s\n",
				mysql_stmt_error(get_throughstation_timeinfo_stmt));
		exit(EXIT_FAILURE);
	}

	if (mysql_stmt_bind_result(get_throughstation_timeinfo_stmt, output_bind)) {
		fprintf(stderr,
				"get_throughstation_timeinfo_stmt mysql_stmt_bind_result() failed\n");
		fprintf(stderr, "%s\n",
				mysql_stmt_error(get_throughstation_timeinfo_stmt));
		exit(EXIT_FAILURE);
	}

	if (mysql_stmt_store_result(get_throughstation_timeinfo_stmt)) {
		fprintf(stderr,
				"get_throughstation_timeinfo_stmt mysql_stmt_store_result() failed\n");
		fprintf(stderr, "%s\n",
				mysql_stmt_error(get_throughstation_timeinfo_stmt));
		exit(EXIT_FAILURE);
	}

	int rownum = mysql_stmt_num_rows(get_throughstation_timeinfo_stmt);
	//printf("rownums: %d\n", rownum);

	if (rownum > 0) {
		while (!mysql_stmt_fetch(get_throughstation_timeinfo_stmt)) {
			StationPassTrainItem item;
			memset(&item, 0, sizeof(item));
			if (!output_is_null[OUTPUT_THROUGHSTATION_TIMEINFO_TRAINID]) {
				sprintf(item.trainID, "%s", output_trainID);
			}
			if (!output_is_null[OUTPUT_THROUGHSTATION_TIMEINFO_STATIONNAME]) {
				sprintf(item.stationName, "%s", output_stationName);
			}
			if (!output_is_null[OUTPUT_THROUGHSTATION_TIMEINFO_STATIONARRIVETIME]) {
				sprintf(item.endTime, "%02d:%02d", output_arriveTime.hour,
						output_arriveTime.minute);
			}
			if (!output_is_null[OUTPUT_THROUGHSTATION_TIMEINFO_STATIONDEPARTTIME]) {
				sprintf(item.startTime, "%02d:%02d", output_departTime.hour,
						output_departTime.minute);
			}

			struct traininfo train;
			memset(&train, 0, sizeof(train));
			bool ishave = true;
			ishave = get_traininfo(item.trainID, &train); //得到一个车次的详细信息
			if (!ishave) { //如果没找到该车次信息，继续下一循环，现实中这种情况不应该存在
				continue;
			}

			bool is_ok = true;
			is_ok = check_train_type(request->trainType, train.trainID); //检查车次是否符合类型条件
			if (!is_ok) {
				continue;
			}

			is_ok = check_depart_arrive_station(request->passStationType,
					&train, input_city, input_city); //检查车次是否符合车站的始发终到要求
			if (!is_ok) {
				continue;
			}

			sprintf(item.departStation, "%s", train.departStation);
			sprintf(item.arriveStation, "%s", train.arriveStation);
			int departTimeHour;
			int departTimeMinute;
			int departTimeSecond;
			sscanf(train.departTime, "%02d:%02d:%02d", &departTimeHour,
					&departTimeMinute, &departTimeSecond);
			sprintf(item.departTime, "%02d:%02d", departTimeHour,
					departTimeMinute);
			int arriveTimeHour;
			int arriveTimeMinute;
			int arriveTimeSecond;
			sscanf(train.arriveTime, "%02d:%02d:%02d", &arriveTimeHour,
					&arriveTimeMinute, &arriveTimeSecond);
			sprintf(item.arriveTime, "%02d:%02d", arriveTimeHour,
					arriveTimeMinute);
			int consumeTimeHour;
			int consumeTimeMinute;
			int consumeTimeSecond;
			sscanf(train.consumeTime, "%02d:%02d:%02d", &consumeTimeHour,
					&consumeTimeMinute, &consumeTimeSecond);
			sprintf(item.consumeTime, "%02d:%02d", consumeTimeHour,
					consumeTimeMinute);

			AddItemStationPassTrain(item, station_pass_trainlist);
		}
		if (ListItemCountStationPassTrain(station_pass_trainlist) == 0) {
			result = false;
		}
	} else {
		result = false;
	}
	return result;
}

//检查一个车次的类型是否符合要求
bool check_train_type(int trainType, const char *trainID) {
	bool result = false;
	char temp_trainID[TRAINS_TRAINID_LEN];
	memset(temp_trainID, 0, sizeof(temp_trainID));
	strncpy(temp_trainID, trainID, strlen(trainID));
	char type = temp_trainID[0];

	if (trainType == TYPE_TRAIN_TYPE_ALL) {
		return true;
	}
	if (trainType & TYPE_TRAIN_TYPE_DG) {
		if (type == 'D' || type == 'G') {
			return true;
		}
	}
	if (trainType & TYPE_TRAIN_TYPE_Z) {
		if (type == 'Z') {
			return true;
		}
	}
	if (trainType & TYPE_TRAIN_TYPE_T) {
		if (type == 'T') {
			return true;
		}
	}
	if (trainType & TYPE_TRAIN_TYPE_K) {
		if (type == 'K') {
			return true;
		}
	}
	if (trainType & TYPE_TRAIN_TYPE_OTHER) {
		if (type != 'D' && type != 'G' && type != 'Z' && type != 'T'
				&& type != 'K') {
			return true;
		}
	}
	return result;
}

//检查一个车次的始发站和终到站是否符合要求
bool check_depart_arrive_station(int passStationType,
		const struct traininfo *train, const char *startStationCity,
		const char *endStationCity) {
	bool result = true;
	struct stationinfo start_station;
	struct stationinfo end_station;
	memset(&start_station, 0, sizeof(start_station));
	memset(&end_station, 0, sizeof(end_station));
//查询发站和到站所在城市
	result = get_stationinfo(train->departStation, &start_station);
	if (!result) {
		return false;
	}
	result = get_stationinfo(train->arriveStation, &end_station);
	if (!result) {
		return false;
	}

	switch (passStationType) {
	case TYPE_PASS_STATION_ALL: {
		break;
	}
	case TYPE_PASS_STATION_BEGIN: {
		if (strcmp(start_station.city, startStationCity) != 0) {
			result = false;
		}
		break;
	}
	case TYPE_PASS_STATION_END: {
		if (strcmp(end_station.city, endStationCity) != 0) {
			result = false;
		}
		break;
	}
	case TYPE_PASS_STATION_BEGIN_END: {
		if (strcmp(start_station.city, startStationCity) != 0
				|| strcmp(end_station.city, endStationCity) != 0) {
			result = false;
		}
		break;
	}
	case TYPE_PASS_STATION_PASS: {
		if (strcmp(start_station.city, startStationCity) == 0
				|| strcmp(end_station.city, endStationCity) == 0) {
			result = false;
		}
		break;
	}
	default: {
		result = false;
		break;
	}
	}
	return result;
}

//查询某个车次的两个车站的发时、到时以及历时
bool get_start_end_consume_timeinfo(const char *trainID,
		const char *startStation, const char *endStation,
		struct stations_timeinfo *timeinfo) {
	bool result = true;
//设置输入的参数信息
	MYSQL_BIND input_bind[INPUT_STATIONS_TIMEINFO_COLUMN_NUM];
	char input_trainID[TRAINS_TRAINID_LEN];
	unsigned long input_trainID_len;
	char input_start_station[TRAINS_DEPARTSTATIONS_LEN];
	unsigned long input_start_station_len;
	char input_end_station[TRAINS_ARRIVESTATIONS_LEN];
	unsigned long input_end_station_len;

	memset(input_bind, 0, sizeof(input_bind));
	input_bind[INPUT_STATIONS_TIMEINFO_STARTSTATION_TRAINID].buffer_type =
			MYSQL_TYPE_STRING;
	input_bind[INPUT_STATIONS_TIMEINFO_STARTSTATION_TRAINID].buffer =
			input_trainID;
	input_bind[INPUT_STATIONS_TIMEINFO_STARTSTATION_TRAINID].buffer_length =
			TRAINS_TRAINID_LEN;
	input_bind[INPUT_STATIONS_TIMEINFO_STARTSTATION_TRAINID].is_null = 0;
	input_bind[INPUT_STATIONS_TIMEINFO_STARTSTATION_TRAINID].length =
			&input_trainID_len;

	input_bind[INPUT_STATIONS_TIMEINFO_ENDSTATION_TRAINID].buffer_type =
			MYSQL_TYPE_STRING;
	input_bind[INPUT_STATIONS_TIMEINFO_ENDSTATION_TRAINID].buffer =
			input_trainID;
	input_bind[INPUT_STATIONS_TIMEINFO_ENDSTATION_TRAINID].buffer_length =
			TRAINS_TRAINID_LEN;
	input_bind[INPUT_STATIONS_TIMEINFO_ENDSTATION_TRAINID].is_null = 0;
	input_bind[INPUT_STATIONS_TIMEINFO_ENDSTATION_TRAINID].length =
			&input_trainID_len;

	input_bind[INPUT_STATIONS_TIMEINFO_STARTSTATION].buffer_type =
			MYSQL_TYPE_STRING;
	input_bind[INPUT_STATIONS_TIMEINFO_STARTSTATION].buffer =
			input_start_station;
	input_bind[INPUT_STATIONS_TIMEINFO_STARTSTATION].buffer_length =
			TRAINS_DEPARTSTATIONS_LEN;
	input_bind[INPUT_STATIONS_TIMEINFO_STARTSTATION].is_null = 0;
	input_bind[INPUT_STATIONS_TIMEINFO_STARTSTATION].length =
			&input_start_station_len;

	input_bind[INPUT_STATIONS_TIMEINFO_ENDSTATION].buffer_type =
			MYSQL_TYPE_STRING;
	input_bind[INPUT_STATIONS_TIMEINFO_ENDSTATION].buffer = input_end_station;
	input_bind[INPUT_STATIONS_TIMEINFO_ENDSTATION].buffer_length =
			TRAINS_ARRIVESTATIONS_LEN;
	input_bind[INPUT_STATIONS_TIMEINFO_ENDSTATION].is_null = 0;
	input_bind[INPUT_STATIONS_TIMEINFO_ENDSTATION].length =
			&input_end_station_len;

	memset(input_trainID, 0, sizeof(input_trainID));
	strncpy(input_trainID, trainID, strlen(trainID));
	input_trainID_len = strlen(input_trainID);

	memset(input_start_station, 0, sizeof(input_start_station));
	strncpy(input_start_station, startStation, strlen(startStation));
	input_start_station_len = strlen(input_start_station);

	memset(input_end_station, 0, sizeof(input_end_station));
	strncpy(input_end_station, endStation, strlen(endStation));
	input_end_station_len = strlen(input_end_station);

//设置输出的参数信息
	MYSQL_BIND output_bind[OUTPUT_STATIONS_TIMEINFO_COLUMN_NUM];
	MYSQL_TIME output_start_station_starttime;
	MYSQL_TIME output_end_station_endtime;
	MYSQL_TIME output_start_station_endtime;
	MYSQL_TIME output_start_station_consumetime;
	MYSQL_TIME output_end_station_consumetime;
	unsigned long output_length[OUTPUT_STATIONS_TIMEINFO_COLUMN_NUM];
	my_bool output_is_null[OUTPUT_STATIONS_TIMEINFO_COLUMN_NUM];
	my_bool output_error[OUTPUT_STATIONS_TIMEINFO_COLUMN_NUM];

	memset(output_bind, 0, sizeof(output_bind));
	output_bind[OUTPUT_STATIONS_TIMEINFO_STARTSTATION_DEPARTTINE].buffer_type =
			MYSQL_TYPE_TIME;
	output_bind[OUTPUT_STATIONS_TIMEINFO_STARTSTATION_DEPARTTINE].buffer =
			&output_start_station_starttime;

	output_bind[OUTPUT_STATIONS_TIMEINFO_ENDSTATION_ARRIVETIME].buffer_type =
			MYSQL_TYPE_TIME;
	output_bind[OUTPUT_STATIONS_TIMEINFO_ENDSTATION_ARRIVETIME].buffer =
			&output_end_station_endtime;

	output_bind[OUTPUT_STATIONS_TIMEINFO_STARTSTATION_ARRIVETIME].buffer_type =
			MYSQL_TYPE_TIME;
	output_bind[OUTPUT_STATIONS_TIMEINFO_STARTSTATION_ARRIVETIME].buffer =
			&output_start_station_endtime;

	output_bind[OUTPUT_STATIONS_TIMEINFO_STARTSTATION_CONSUMETIME].buffer_type =
			MYSQL_TYPE_TIME;
	output_bind[OUTPUT_STATIONS_TIMEINFO_STARTSTATION_CONSUMETIME].buffer =
			&output_start_station_consumetime;

	output_bind[OUTPUT_STATIONS_TIMEINFO_ENDSTATION_CONSUMETIME].buffer_type =
			MYSQL_TYPE_TIME;
	output_bind[OUTPUT_STATIONS_TIMEINFO_ENDSTATION_CONSUMETIME].buffer =
			&output_end_station_consumetime;

	int i = 0;
	for (i = 0; i < OUTPUT_STATIONS_TIMEINFO_COLUMN_NUM; i++) {
		output_bind[i].is_null = &output_is_null[i];
		output_bind[i].length = &output_length[i];
		output_bind[i].error = &output_error[i];
	}

	if (mysql_stmt_bind_param(get_stations_timeinfo_stmt, input_bind)) {
		fprintf(stderr,
				"get_stations_timeinfo_stmt mysql_stmt_bind_param() failed\n");
		fprintf(stderr, "%s\n", mysql_stmt_error(get_stations_timeinfo_stmt));
		exit(EXIT_FAILURE);
	}

	if (mysql_stmt_execute(get_stations_timeinfo_stmt)) {
		fprintf(stderr,
				"get_stations_timeinfo_stmt mysql_stmt_execute(), failed\n");
		fprintf(stderr, "%s\n", mysql_stmt_error(get_stations_timeinfo_stmt));
		exit(EXIT_FAILURE);
	}

	if (mysql_stmt_bind_result(get_stations_timeinfo_stmt, output_bind)) {
		fprintf(stderr,
				"get_stations_timeinfo_stmt mysql_stmt_bind_result() failed\n");
		fprintf(stderr, "%s\n", mysql_stmt_error(get_stations_timeinfo_stmt));
		exit(EXIT_FAILURE);
	}

	if (mysql_stmt_store_result(get_stations_timeinfo_stmt)) {
		fprintf(stderr,
				"get_stations_timeinfo_stmt mysql_stmt_store_result() failed\n");
		fprintf(stderr, "%s\n", mysql_stmt_error(get_stations_timeinfo_stmt));
		exit(EXIT_FAILURE);
	}

	int rownum = mysql_stmt_num_rows(get_stations_timeinfo_stmt);
//printf("rownums: %d\n", rownum);

	if (rownum > 0) {
		while (!mysql_stmt_fetch(get_stations_timeinfo_stmt)) {
			int start_station_departtime_hour;
			int start_station_departtime_minute;
			int start_station_arrivetime_hour;
			int start_station_arrivetime_minute;
			int start_station_consume_time_hour;
			int start_station_consume_time_minute;
			int end_station_consume_time_hour;
			int end_station_consume_time_minute;

			if (!output_is_null[OUTPUT_STATIONS_TIMEINFO_STARTSTATION_DEPARTTINE]) {
				sprintf(timeinfo->startTime, "%02d:%02d",
						output_start_station_starttime.hour,
						output_start_station_starttime.minute);
				start_station_departtime_hour =
						output_start_station_starttime.hour;
				start_station_departtime_minute =
						output_start_station_starttime.minute;
			}

			if (!output_is_null[OUTPUT_STATIONS_TIMEINFO_ENDSTATION_ARRIVETIME]) {
				sprintf(timeinfo->endTime, "%02d:%02d",
						output_end_station_endtime.hour,
						output_end_station_endtime.minute);
			};

			if (!output_is_null[OUTPUT_STATIONS_TIMEINFO_STARTSTATION_ARRIVETIME]) {
				start_station_arrivetime_hour =
						output_start_station_endtime.hour;
				start_station_arrivetime_minute =
						output_start_station_endtime.minute;
			} else { //如果发站的到达时间为空，说明该发站为车次的始发站，将发站到达时间设置成出发时间
				start_station_arrivetime_hour = start_station_departtime_hour;
				start_station_arrivetime_minute =
						start_station_departtime_minute;
			}

			if (!output_is_null[OUTPUT_STATIONS_TIMEINFO_STARTSTATION_CONSUMETIME]) {
				start_station_consume_time_hour =
						output_start_station_consumetime.hour;
				start_station_consume_time_minute =
						output_start_station_consumetime.minute;
			} else { //如果发展的耗时为空，说明该发站为车次的始发站，将耗时设置为0
				start_station_consume_time_hour = 0;
				start_station_consume_time_minute = 0;
			}

			if (!output_is_null[OUTPUT_STATIONS_TIMEINFO_ENDSTATION_CONSUMETIME]) {
				end_station_consume_time_hour =
						output_end_station_consumetime.hour;
				end_station_consume_time_minute =
						output_end_station_consumetime.minute;
			}

			int depart_subtract_arrive = (start_station_departtime_hour * 60
					+ start_station_departtime_minute)
					- (start_station_arrivetime_hour * 60
							+ start_station_arrivetime_minute); //发站的停靠时间，以分钟为单位
			int consume_subtract = (end_station_consume_time_hour * 60
					+ end_station_consume_time_minute)
					- (start_station_consume_time_hour * 60
							+ start_station_consume_time_minute); //到站和发站之间的时间，包括发站的停靠时间，以分钟为单位
			int timelast = consume_subtract - depart_subtract_arrive; //减去停靠时间

			int timelast_hour = timelast / 60;
			int timelast_minute = timelast % 60;

			sprintf(timeinfo->consumeTime, "%02d:%02d", timelast_hour,
					timelast_minute);

			sprintf(timeinfo->startStation, "%s", input_start_station);
			sprintf(timeinfo->endStation, "%s", input_end_station);
			break;
		}
	} else {
		result = false;
	}
	return result;
}

//查询某个车站的基本信息，成功返回true并将结果添加到stationinfo中，失败返回false
bool get_stationinfo(const char *stationname, struct stationinfo *station) {
	bool result = true;
//设置输入的参数信息
	MYSQL_BIND input_bind[1];
	char input_stationname[STATIONS_STATION_NAME_LEN];
	unsigned long input_stationname_len;

	memset(input_bind, 0, sizeof(input_bind));
	input_bind[TABLE_STATIONS_SELECT_STATION_NAME].buffer_type =
			MYSQL_TYPE_STRING;
	input_bind[TABLE_STATIONS_SELECT_STATION_NAME].buffer = input_stationname;
	input_bind[TABLE_STATIONS_SELECT_STATION_NAME].buffer_length =
			STATIONS_STATION_NAME_LEN;
	input_bind[TABLE_STATIONS_SELECT_STATION_NAME].is_null = 0;
	input_bind[TABLE_STATIONS_SELECT_STATION_NAME].length =
			&input_stationname_len;

	memset(input_stationname, 0, sizeof(input_stationname));
	strncpy(input_stationname, stationname, strlen(stationname));
	input_stationname_len = strlen(input_stationname);

//设置输出的参数信息
	MYSQL_BIND output_bind[TABLE_STATIONS_SELECT_COLUMN_NUM];
	char output_stationname[STATIONS_STATION_NAME_LEN];
	char output_city[STATIONS_CITY_LEN];
	char output_startSaleTime[STATIONS_START_SALE_TIME_LEN];
	unsigned long output_length[TABLE_STATIONS_SELECT_COLUMN_NUM];
	my_bool output_is_null[TABLE_STATIONS_SELECT_COLUMN_NUM];
	my_bool output_error[TABLE_STATIONS_SELECT_COLUMN_NUM];

	memset(output_bind, 0, sizeof(output_bind));
	output_bind[TABLE_STATIONS_SELECT_STATION_NAME].buffer_type =
			MYSQL_TYPE_STRING;
	output_bind[TABLE_STATIONS_SELECT_STATION_NAME].buffer = output_stationname;
	output_bind[TABLE_STATIONS_SELECT_STATION_NAME].buffer_length =
			STATIONS_STATION_NAME_LEN;

	output_bind[TABLE_STATIONS_SELECT_CITY].buffer_type = MYSQL_TYPE_STRING;
	output_bind[TABLE_STATIONS_SELECT_CITY].buffer = output_city;
	output_bind[TABLE_STATIONS_SELECT_CITY].buffer_length = STATIONS_CITY_LEN;

	output_bind[TABLE_STATIONS_SELECT_STARTSALETIME].buffer_type =
			MYSQL_TYPE_STRING;
	output_bind[TABLE_STATIONS_SELECT_STARTSALETIME].buffer =
			output_startSaleTime;
	output_bind[TABLE_STATIONS_SELECT_STARTSALETIME].buffer_length =
			STATIONS_START_SALE_TIME_LEN;

	int i = 0;
	for (i = 0; i < TABLE_STATIONS_SELECT_COLUMN_NUM; i++) {
		output_bind[i].is_null = &output_is_null[i];
		output_bind[i].length = &output_length[i];
		output_bind[i].error = &output_error[i];
	}

	if (mysql_stmt_bind_param(get_stationinfo_stmt, input_bind)) {
		fprintf(stderr,
				"get_stationinfo_stmt mysql_stmt_bind_param() failed\n");
		fprintf(stderr, "%s\n", mysql_stmt_error(get_stationinfo_stmt));
		exit(EXIT_FAILURE);
	}

	if (mysql_stmt_execute(get_stationinfo_stmt)) {
		fprintf(stderr, "get_stationinfo_stmt mysql_stmt_execute(), failed\n");
		fprintf(stderr, "%s\n", mysql_stmt_error(get_stationinfo_stmt));
		exit(EXIT_FAILURE);
	}

	if (mysql_stmt_bind_result(get_stationinfo_stmt, output_bind)) {
		fprintf(stderr,
				"get_stationinfo_stmt mysql_stmt_bind_result() failed\n");
		fprintf(stderr, "%s\n", mysql_stmt_error(get_stationinfo_stmt));
		exit(EXIT_FAILURE);
	}

	if (mysql_stmt_store_result(get_stationinfo_stmt)) {
		fprintf(stderr,
				"get_stationinfo_stmt mysql_stmt_store_result() failed\n");
		fprintf(stderr, "%s\n", mysql_stmt_error(get_stationinfo_stmt));
		exit(EXIT_FAILURE);
	}

	int rownum = mysql_stmt_num_rows(get_stationinfo_stmt);
//printf("rownums: %d\n", rownum);

	if (rownum > 0) {
		while (!mysql_stmt_fetch(get_stationinfo_stmt)) {
			if (!output_is_null[TABLE_STATIONS_SELECT_STATION_NAME]) {
				sprintf(station->stationName, "%s", output_stationname);
			}

			if (!output_is_null[TABLE_STATIONS_SELECT_CITY]) {
				sprintf(station->city, "%s", output_city);
			}

			if (!output_is_null[TABLE_STATIONS_SELECT_STARTSALETIME]) {
				sprintf(station->startSaleTime, "%s", output_startSaleTime);
			}
			break;
		}
	} else {
		result = false;
	}

	return result;
}

//查询车站的车票起售时间，成功返回true，失败返回false
bool query_start_sale_time(struct stationinfo *station) {
	bool result = true;
	char stationname[STATIONS_STATION_NAME_LEN];
	memset(stationname, 0, sizeof(stationname));
	strncpy(stationname, station->stationName, strlen(station->stationName));

	memset(station, 0, sizeof(*station));
	result = get_stationinfo(stationname, station);

	return result;
}
